package me.alide.modules.survey.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import me.alide.common.exception.RRException;
import me.alide.common.utils.R;
import me.alide.modules.survey.entity.MySurvey;
import me.alide.modules.survey.entity.OrgInfoEntity;
import me.alide.modules.survey.entity.SurveyEntity;
import me.alide.modules.survey.form.StatusForm;
import me.alide.modules.survey.form.SurveyDetailRes;
import me.alide.modules.survey.form.SurveyForm;
import me.alide.modules.survey.service.OrgInfoService;
import me.alide.modules.survey.service.SurveyService;
import me.alide.modules.sys.controller.AbstractController;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static me.alide.common.utils.Constant.DATA;

/**
 * @author: Anzhen
 * @description:
 * @PROJECT_NAME: alide
 * @create: 2021-07-25 14:31
 **/
@Slf4j
@RestController
@RequestMapping("/survey")
public class SurveyController extends AbstractController {

    @Resource
    private SurveyService surveyService;


    @Resource
    private OrgInfoService orgInfoService;

    /**
     * 增
     * @param surveyForm
     * @return
     */
    @RequestMapping("save")
    public R save(@RequestBody SurveyForm surveyForm) {
        try {
            SurveyForm.OrgInfo orgInfo = surveyForm.getOrgInfo();
            OrgInfoEntity orgInfoEntity = orgInfoService.queryById(orgInfo.getId());
            orgInfoEntity.setContact(orgInfo.getContact());
            orgInfoEntity.setContactJob(orgInfo.getContactJob());
            orgInfoEntity.setContactPhone(orgInfo.getContactPhone());
            orgInfoEntity.setDept(orgInfo.getDept());
            orgInfoEntity.setLeader(orgInfo.getLeader());
            orgInfoEntity.setLeaderJob(orgInfo.getLeaderJob());
            orgInfoEntity.setLeaderPhone(orgInfo.getLeaderPhone());
            orgInfoEntity.setLeaderContact(orgInfo.getLeaderContact());
            orgInfoEntity.setLeaderContactJob(orgInfo.getLeaderContactJob());
            orgInfoEntity.setLeaderContactPhone(orgInfo.getLeaderContactPhone());
            orgInfoService.update(orgInfoEntity);

            SurveyForm.Survey survey = surveyForm.getSurvey();
            SurveyEntity surveyEntity = new SurveyEntity();
            BeanUtil.copyProperties(survey, surveyEntity);
            surveyEntity.setOrgId(orgInfoEntity.getId());
            surveyEntity.setCreateTime(new Date());
            surveyEntity.setSurveyUserId(getUserId());
            surveyService.save(surveyEntity);
            return R.ok();
        } catch (Exception e) {
            log.error("保存失败", e);
            return R.error(500, e.getMessage());
        }
    }

    /**
     * 删
     * @param ids
     * @return
     */
    @RequestMapping("delete")
    public R delete(@RequestBody Long[] ids) {
        try {
            int deleteCount = surveyService.delete(ids);
            if (deleteCount == 1) {
                log.info("删除成功{}条", deleteCount);
            } else {
                throw new RRException("删除失败", 500);
            }
        } catch (Exception e) {
            log.error("删除失败", e);
            return R.error(500, e.getMessage());
        }
        return R.ok();
    }

    /**
     * 改
     */
    @RequestMapping("update")
    public R update(@RequestBody SurveyEntity surveyEntity) {
        try {
            int save = surveyService.update(surveyEntity);
            if (save == 1) {
                log.info("更新成功");
            } else {
                throw new RRException("更新失败", 500);
            }
        } catch (Exception e) {
            log.error("更新成功", e);
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    /**
     * 查
     */
    @RequestMapping("queryAll")
    public R queryAll() {
        try {
            return R.ok().put(DATA, surveyService.queryAll());
        } catch (Exception e) {
            log.error("查询失败", e);
            return R.error();
        }
    }

    /**
     * 查
     */
    @RequestMapping("queryById")
    public SurveyEntity queryById(@RequestParam Long id) {
        try {
            return surveyService.queryById(id);
        } catch (Exception e) {
            log.error("查询失败", e);
            return null;
        }
    }

    /**
     * 查
     */
    @RequestMapping("list")
    public R queryBySurveyUserId(Long userId, int page, int pageSize) {
        try {
            List<MySurvey> mySurveyList = surveyService.queryBySurveyUserId(userId, page, pageSize);
            return R.ok().put(DATA, mySurveyList);
        } catch (Exception e) {
            log.error("查询失败", e);
            return R.error();
        }
    }

    @RequestMapping("all")
    public R surveyList(int page, int pageSize) {
        try {
            List<MySurvey> mySurveyList = surveyService.queryBySurveyList(page, pageSize);
            return R.ok().put(DATA, mySurveyList);
        } catch (Exception e) {
            log.error("查询失败", e);
            return R.error();
        }
    }


    @GetMapping("detail")
    public R detail(Long id) {
       SurveyEntity surveyEntity = surveyService.queryById(id);
       if(surveyEntity == null) return R.error();
       OrgInfoEntity orgInfoEntity = orgInfoService.queryById(surveyEntity.getOrgId());
       return R.ok().put(DATA, new SurveyDetailRes(surveyEntity, orgInfoEntity));
    }

    @PostMapping("updateStatus")
    public R statusUpdate(@RequestBody StatusForm status) {
        try{
            surveyService.updateStatus(status);
        }catch (Exception e) {
            log.error("更新失败", e);
            return R.error();
        }
        return R.ok();
    }
}
